/* ----------------------------------------------------------------------------------------------------------------
 * TiledWorld
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.world 
{
	import flash.geom.Point;
	import pure.engine.system.ns_despair;
	use namespace ns_despair;
	/**
	 * Tile世界
	 * @author Pure
	 */
public class TiledWorldContent extends WorldContent 
{
	/**
	 * @param landformData
	 * @param tileSize
	 */
	public function TiledWorldContent(landformData:Array, tileSize:int) 
	{
		super();
		
		_tileSize = tileSize;
		
		_landformData = landformData;
		_columns = landformData[0].length;
		_rows = landformData.length;
		
		m_width = _columns * _tileSize;
		m_height = _rows * _tileSize;
	}
	
	
	
	// ----------------------------------［ Model ］----------------------------------
	
	protected var _landformData:Array;
	
	protected var _columns:int, _rows:int;
		
	protected var _tileSize:int;
	
	
	// ----------------------------------［ Const ］----------------------------------

	protected static var cachedPointB:Point = new Point();
	
	
	
	
	/**
	 * [取]获取地形
	 * 
	 * @param wx
	 * @param wy
	 */
	public function getLandform(wx:int, wy:int):int
	{
		var tileX:int = _getTileX(wx);
		var tileY:int = _getTileY(wy);
		
		if (tileX == -1 || tileY == -1)
		{
			return -1;
		}
		
		return _landformData[tileY][tileX];
	}
	
	
	/**
	 * [存]改变地形
	 * 
	 * @param wx
	 * @param wy
	 * @param tile
	 */
	public function setLandform(wx:int, wy:int, tile:int):void 
	{
		var tileX:int = _getTileX(wx);
		var tileY:int = _getTileY(wy);
		
		if (tileX == -1 || tileY == -1)
		{
			return;
		}
		
		_landformData[tileY][tileX] = tile;
	}

	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  get × set  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	
	 
	public function get tileSize():int { return _tileSize; }
	 
	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	
	 
	 
	/**
	 * 转换获取世界X轴Tile值
	 * 
	 * @param wx
	 */
	protected function _getTileX(wx:Number):int 
	{
		var tileX:int = Math.floor(wx / _tileSize);
		if (tileX == _columns)
		{
			tileX--;
		}
		else if (tileX<0||tileX>_columns)
		{
			tileX = -1;
		}
		
		return tileX;
	}
	 
	
	/**
	 * 转换获取世界Y轴Tile值
	 * 
	 * @param wy
	 */
	protected function _getTileY(wy:Number):int
	{
		var tileY:int = Math.floor(wy / _tileSize);
		if (tileY == this._rows)
		{
			tileY--;
		}
		else if (tileY<0||tileY>this._rows)
		{
			tileY = -1;
		}
		
		return tileY;
	}
	

	
}

}